# 1、创建一个名为 X 并继承自 Y 的类
# class X(Y)

# 2、 类 X 有一个带有 self 和 J 参数的 __init__ 函数
# class X(object):  def __init__(self,J):

# 3、类 X 有一个带有 self 和 J 参数的 M 函数
# class X(object):   def M(self,J):

# 4、设 foo 为类 X 的一个实例
# foo = X()

# 5、从 foo 那里获取 M 函数，并用 self 和 J 参数来调用它
# foo.M(J)

# 6、从 foo 那里获取 K 属性，并设它为 Q
# foo.K = Q



import random
# import random
from urllib.request import urlopen
# from urllib.request import urlopen
import sys
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []

PHRASES = {
    "class %%%(%%%):":
        "Make a class named %%% that is-a %%%.",
    "class %%%(object):\n\tdef __init__(self, ***)":
        "class %%% has-a __init__ that takes self and *** params.",
    "class %%%(object):\n\tdef ***(self, @@@)":
        "class %%% has-a function *** that takes self and @@@ params.",
    "*** = %%%()":
        "Set *** to an instance of class %%%.",
    "***.***(@@@)":
        "From *** get the *** function, call it with params self @@@.",
    "***.*** = '***'":
        "From *** get the *** attribute and set it to '***'."
}

# do they want to drill phrases first
if len(sys.argv) == 2 and sys.argv[1] == "english":
# if len(sys.argv) == 2 and sys.argv[1] == "english":
    PHRASE_FIRST = True
else:
    PHRASE_FIRST = False

context = ssl._create_unverified_context()
# load up the words from the website
for word in urlopen(WORD_URL, context=context).readlines():
# for word in urlopen(WORD_URL).readlines():
    WORDS.append(str(word.strip(),encoding="utf-8"))
    # WORDS.append(str(word.strip(), encoding="utf-8"))

def convert(snippet,phrase):
 # def convert(snippet, phrase):
    class_names = [w.capitalize() for w in
   # class_names = [w.capitalize() for w in
    random.sample(WORDS,snippet.count("%%%"))]
    # random.sample(WORDS, snippet.count("%%%"))]
    other_names = random.sample(WORDS,snippet.count("***"))
    # other_names = random.sample(WORDS, snippet.count("***"))
    results = []
    # results = []
    param_names = []
    # param_names = []

    for i in range(0,snippet.count("@@@")):
    # for i in range(0, snippet.count("@@@")):
        param_count = random.randint(1,3)
        # param_count = random.randint(1,3)
        param_names.append(', '.join(
        # param_names.append(', '.join(
            random.sample(WORDS,param_count)))
            # random.sample(WORDS, param_count)))

    for sentence in snippet, phrase:
    # for sentence in snippet, phrase:
        result = sentence[:]
        # result = sentence[:]

        # fake class names
        for word in class_names:
        # for word in class_names:
            result = result.replace("%%%", word, 1)
            # result = result.replace("%%%", word, 1)

        # fake other names
        for word in other_names:
        # for word in other_names:
            result = result.replace("***", word, 1)
            # result = result.replace("***", word, 1)

        # fake parameter lists
        for word in param_names:
        # for word in param_names:
            result = result.replace("@@@", word, 1)
            # result = result.replace("@@@", word, 1)

        results.append(result)
        # results.append(result)

    return results
    # return results


# keep going until they hit CTRL-D
try:
    while True:
        snippets = list(PHRASES.keys())
        # snippets = list(PHRASES.keys())
        random.shuffle(snippets)
        # random.shuffle(snippets)

        for snippet in snippets:
        # for snippet in snippets:
            phrase = PHRASES[snippet]
            # phrase = PHRASES[snippet]
            question, answer = convert(snippet, phrase)
            # question, answer = convert(snippet, phrase)
            if PHRASE_FIRST:
            # if PHRASE_FIRST:
                question, answer = answer, question
                # question, answer = answer, question

            print(question)
            # print(question)

            input("> ")
            # input("> ")
            print(f"ANSWER:{answer}\n\n")
            # print(f"ANSWER: {answer}\n\n")
except EOFError:
# except EOFError:
    print("\nBye")
    # print("\nBye")



